package system.security.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import system.security.dao.UserDao;
import system.security.entity.User;
import system.security.service.UserService;


@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Transactional(readOnly=true)
	@Override
	public User getById(int userId) {
		User user = userDao.get(userId);
		return user;
	}

	@Override
	public User findByName(String username) {
			List<User> list = userDao.findByProperty("username", username);
			if(!list.isEmpty()){
				return list.get(0);
			}
			return null;		
	}

	@Override
	@Transactional(readOnly = true)
	public List<User> getAll() {
		return userDao.list();
	}
	
	@Override
	public boolean usernameExists(String username, int userId) {
		List<User> list = userDao.findByProperty("username", username);
		return list.isEmpty();
	}
	
	@Override
	public void save(User user) throws Exception {
			userDao.saveOrUpdate(user);
	}

	@Override
	public void delete(int userId) {
		userDao.remove(userId);
	}
	
	@Override
	public void delete(User user){
		userDao.remove(user);		
	}
	
}
